import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12

Item {

    id: root

    // public ==================================================================
    property int rowHeight: 40*dp
    property int titleHeight: 50*dp

    Component.onCompleted: {
        for(let i = 0; i < 96; i ++){
            tableData.push([])
            for(let j = 0; j < 7; j ++){
                tableData.push(0)
            }
        }
    }

    function setData(data){

        selectedRange1 = -1
        selectedRange2 = -1
        tableData = data
        if(rowCount!==data.length){ // 行数发生变化时，清除滚动状态
//            console.log("行数发生变化，清除滚动状态")
            tableView.contentY = 0 + tableView.originY
        }else{
//            console.log("行数没有发生变化，不用清除滚动状态")
        }

        columnCount = title.length
        rowCount = data.length
    }
    function setVisibleNum(num){
        columnVisibleNum = num
    }
    function resortData(index){

        CAnalysis.updateSampleTableSorted(index)

        reloadAnalysisData()
    }


    // private =================================================================


    property var title: ["孔位", "样本名称", "样本类型", "荧光染料", "CT", "浓度", "平均浓度"]
    property var columnVisibleNum: 7        // 目前只隐藏后面几列，因此只记录显示的列数
    property var tableData: []

    property int columnCount: 7
    property int rowCount: 0

    property bool isSelecting: false
    property int selectedRange1: 0
    property int selectedRange2: 0

    property int hoverIdx: -1

    function setSelectedRange(range1, range2){

        var selectedList = []
        for(let i = range1; i <= range2; i ++){
            selectedList.push(tableData[i][3] + "_" + tableData[i][0])
        }
        CAnalysis.updateSampleTableSelected(selectedList)
        reloadChartData()
    }

    // 样式 ==================================================================
    anchors.fill: parent

    // 表头
    Rectangle{
        width: parent.width;  height: titleHeight
        color: "#F7F7FA"
        Row{
            Repeater{
                model: columnCount
                Item{
                    visible: index < columnVisibleNum
                    height: titleHeight
                    width: root.width / columnVisibleNum
                    Text{
                        text: title[index]
                        anchors.centerIn: parent
                        font.pixelSize: config.fontsize
                    }Image{     // 排序
                        height: 30*dp; width: 30*dp
                        anchors.centerIn: parent
                        anchors.horizontalCenterOffset: parent.children[0].contentWidth/2 + 8*dp
                        source: "qrc:/picture/sortTable.png"
                        visible: false
                        opacity: 0.2
                    }
                    MouseArea{
                        height: 30*dp
                        width: parent.children[0].contentWidth + 10*dp
                        anchors.centerIn: parent
                        cursorShape: Qt.PointingHandCursor
                        hoverEnabled: true
                        onEntered: {
                            parent.children[1].visible = true
                        }onExited: {
                            parent.children[1].visible = false
                        }
                        onReleased: {
                            resortData(index)
                        }
                    }
                }
            }
        }
    }

    // 表格
    ListView{
        id: tableView
        clip: true
        y: titleHeight
        width: root.width;    height: root.height - titleHeight
        model: tableData.length
        delegate: lineDelegate
        interactive: false
        ScrollBar.vertical: ScrollBar{ id: scrollBar; policy: ScrollBar.AlwaysOn }
        MouseArea{
            anchors.fill: parent
            hoverEnabled: true
            onPressed: {
                isSelecting = true
                var mousePos = mapToItem(tableView, mouseX, mouseY)
                selectedRange1 = tableView.itemAt(0, mousePos.y+tableView.contentY).idx
                selectedRange2 = tableView.itemAt(0, mousePos.y+tableView.contentY).idx

                setSelectedRange(Math.min(selectedRange1, selectedRange2), Math.max(selectedRange1, selectedRange2))
            }onReleased: {
                isSelecting = false
                var mousePos = mapToItem(tableView, mouseX, mouseY)
                selectedRange2 = tableView.itemAt(0, Math.max(0, Math.min(mousePos.y, tableView.height-1))+tableView.contentY).idx
                timer_down.stop()
                timer_up.stop()
            }onPositionChanged: {
                var mousePos = mapToItem(tableView, mouseX, mouseY)
                hoverIdx = tableView.indexAt(0, mousePos.y+tableView.contentY)
                if(!isSelecting){
                    return
                }
                timer_down.stop()
                timer_up.stop()
                if(rowCount*rowCount>tableView.height){
                    if(mousePos.y > tableView.height){
                        tableView.contentY = Math.min(tableView.contentY-tableView.originY+5, rowCount*rowHeight-tableView.height) + tableView.originY
                        timer_down.mousePos = mousePos
                        timer_down.start()
                    }if(mousePos.y < 0){
                        timer_up.mousePos = mousePos
                        tableView.contentY = Math.max(0, tableView.contentY-tableView.originY-5) + tableView.originY
                        timer_up.start()
                    }
                }
                selectedRange2 = tableView.itemAt(0, Math.max(0, Math.min(mousePos.y, Math.min(tableView.height, rowCount*rowHeight)-1))+tableView.contentY).idx


                setSelectedRange(Math.min(selectedRange1, selectedRange2), Math.max(selectedRange1, selectedRange2))
            }onWheel: {
                if(rowCount*rowHeight>tableView.height)
                    tableView.contentY = Math.max(0, Math.min(rowCount*rowHeight-tableView.height, tableView.contentY-tableView.originY-wheel.angleDelta.y*0.5*dp)) + tableView.originY
            }
        }

    }

    onVisibleChanged: {
        if(!visible){
            timer_down.stop()
            timer_up.stop()
        }
    }

    Timer{
        id: timer_down
        property var mousePos
        interval: 20
        repeat: true
        onTriggered: {
            tableView.contentY = Math.min(tableView.contentY-tableView.originY+10, rowCount*rowHeight-tableView.height) + tableView.originY
            selectedRange2 = tableView.itemAt(0, Math.min(timer_down.mousePos.y, tableView.height-1)+tableView.contentY).idx

        }
    }Timer{
        id: timer_up
        property var mousePos
        interval: 20
        repeat: true
        onTriggered: {
            tableView.contentY = Math.max(0, tableView.contentY-tableView.originY-10) + tableView.originY
            selectedRange2 = tableView.itemAt(0, Math.max(timer_up.mousePos.y, 0)+tableView.contentY).idx
        }
    }


    Component{
        id: lineDelegate
        Rectangle{
            property int idx: index
            height: rowHeight
            width: root.width
            Row{
                Repeater{
                    model: columnVisibleNum
                    Rectangle{
                        visible: index < columnVisibleNum
                        color: (idx>=Math.min(selectedRange1,selectedRange2)&&idx<=Math.max(selectedRange1,selectedRange2)) ? "#F2F9FF" : (hoverIdx===idx?"#F8FCFF":"white")
                        width: root.width / columnVisibleNum
                        height: rowHeight
                        Text{
                            anchors.centerIn: parent
                            font.pixelSize: config.fontsize
                            text: tableData[idx][index]
                        }
                    }
                }
            }

            Rectangle{ height: 1; width: parent.width; y: parent.height-1; color: "#EFEFEF" }
        }
    }

}
